#  Copyright (c) 1997-2009
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#  $Project: polymake $$Id: formatting_filters 9298 2009-09-02 23:10:56Z gawrilow $


# category: Filters
# args: property [, row_labels ] [, col_labels ]
# @a row_labels and/or col_labels should be of type Array<String> or compatible
# Each item in the @a property will be prepended with corresponding label and a colon.
#
# @a property is expected to be of type @see type.set or @see type.incidence_matrix.
#
# If @a row_labels are omitted, takes the ordinal numbers starting with 0.

user_function rows_labeled($;$$) {
   my ($data, $row_labels, $col_labels)=@_;
   if (defined $row_labels) {
      is_array($row_labels) or croak( "row_labels must be an array" );
   }
   if (defined $col_labels) {
      is_array($col_labels) or croak( "col_labels must be an array" );
   }
   my $i=-1;
   map {
      ++$i;
      (defined($row_labels) ? $row_labels->[$i] : $i) . ":" . join(" ", defined($col_labels) ? @$col_labels[@$_] : @$_) . "\n"
   } @$data;
}

# for convenience: a PTL Graph is not a container

user_function rows_labeled(Graph;$$) {
   rows_labeled(adjacency_matrix(shift),@_);
}

# category: Filters
# args: property
# Equivalent to @see rows_labeled with omitted @a labels argument.
# Previously called "numbered".

user_function rows_numbered {
   &rows_labeled;
}


# category: Filters
# args: vector
# Produce a histogram of a vector: each different element value is mapped on the number of its occurences

user_function histogram {
   my ($data)=@_;
   return unless @$data;
   my $element_type=Core::PropertyType::guess_element_type($data);
   my $map=Map->type($element_type, Int->type)->new_object;
   ++$map->{$_} for @$data;
   $map
}

##################################################################################

# category: Filters
# args: IncidenceMatrix
# Convert to a dense 0/1 matrix

user_function dense(IncidenceMatrix) { dense(toMatrix<Int>(@_)); }

# category: Filters
# args: Set, dim
# Convert to a dense 0/1 vector of a given dimension

user_function dense(Set $) { dense(toVector<Int>(@_)); }


# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
